package com.songlanyun.common.kqUtils;

import java.io.IOException;
import java.io.InputStream;
import java.io.UnsupportedEncodingException;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

/**
 * 字符串辅助类
 * @ClassName: StringUtils
 * @author juntao.gao
 *
 */
public class StringUtils {

	private static final Log LOGGER = LogFactory.getLog(StringUtils.class);

	/**默认空串*/
	public static final String EMPTY_STRING = " ";

	private StringUtils() {
	}

	/**
	 * �?查指定的字符串是否为空�??
	 * <ul>
	 * <li>SysUtils.isEmpty(null) = true</li>
	 * <li>SysUtils.isEmpty("") = true</li>
	 * <li>SysUtils.isEmpty("   ") = true</li>
	 * <li>SysUtils.isEmpty("abc") = false</li>
	 * </ul>
	 * 
	 * @param value
	 *            待检查的字符�?
	 * @return true/false
	 */
	public static boolean isEmpty(Object value) {
		int strLen;
		if (value == null || (strLen = value.toString().length()) == 0) {
			return true;
		}
		for (int i = 0; i < strLen; i++) {
			if (!Character.isWhitespace(value.toString().charAt(i))) {
				return false;
			}
		}
		return true;
	}
	
    public static String getPostData(InputStream in, int size, String charset) {
        if (in != null && size > 0) {
            byte[] buf = new byte[size];
            try {
                in.read(buf);
                if (charset == null || charset.length() == 0)
                    return new String(buf);
                else {
                    return new String(buf, charset);
                }
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
        return null;
    } 

	/**
	 * �?查指定的字符串是否不为空�?
	 *<ul>
	 * <li>SysUtils.isNotEmpty(null) = false</li>
	 * <li>SysUtils.isNotEmpty("") = false</li>
	 * <li>SysUtils.isNotEmpty("   ") = false</li>
	 * <li>SysUtils.isNotEmpty("abc") = true</li>
	 * </ul>
	 * 
	 * @param value
	 * 			待检查的字符�?
	 * @return true/false
	 */
//	public static boolean isNotEmpty(String value) {
//		return !isEmpty(value);
//	}
	public static boolean isNotEmpty(Object value) {
		return !isEmpty(value);
	}
	/**
	 * �?查对象是否为数字型字符串,包含负数�?头的�?
	 */
	public static boolean isNumeric(Object obj) {
		if (obj == null) {
			return false;
		}
		char[] chars = obj.toString().toCharArray();
		int length = chars.length;
		if (length < 1)
			return false;

		int i = 0;
		if (length > 1 && chars[0] == '-')
			i = 1;

		for (; i < length; i++) {
			if (!Character.isDigit(chars[i])) {
				return false;
			}
		}
		return true;
	}

	/**
	 * �?查指定的字符串列表是否不为空�?
	 */
	public static boolean areNotEmpty(String... values) {
		boolean result = true;
		if (values == null || values.length == 0) {
			result = false;
		} else {
			for (String value : values) {
				result &= !isEmpty(value);
			}
		}
		return result;
	}

	/**
	 * 把�?�用字符编码的字符串转化为汉字编码�??
	 */
	public static String unicodeToChinese(String unicode) {
		StringBuilder out = new StringBuilder();
		if (!isEmpty(unicode)) {
			for (int i = 0; i < unicode.length(); i++) {
				out.append(unicode.charAt(i));
			}
		}
		return out.toString();
	}

	/**
	 * 过滤不可见字�?
	 */
	public static String stripNonValidXMLCharacters(String input) {
		if (input == null || ("".equals(input)))
			return "";
		StringBuilder out = new StringBuilder();
		char current;
		for (int i = 0; i < input.length(); i++) {
			current = input.charAt(i);
			if ((current == 0x9) || (current == 0xA) || (current == 0xD) || ((current >= 0x20) && (current <= 0xD7FF))
					|| ((current >= 0xE000) && (current <= 0xFFFD)))
				out.append(current);
		}
		return out.toString();
	}

	/** 
	 * 去除字符串中�?包含的空格（包括:空格(全角，半�?)、制表符、换页符等） 
	 * @param s 
	 * @return 
	 */
	public static String removeAllBlank(String s) {
		String result = "";
		if (null != s && !"".equals(s)) {
			result = s.replaceAll("[�?*| *| *|\\s*]*", "");
		}
		return result;
	}

	/** 
	 * 去除字符串中头部和尾部所包含的空格（包括:空格(全角，半�?)、制表符、换页符等） 
	 * @param s 
	 * @return 
	 */
	public static String trim(String s) {
		String result = "";
		if (null != s && !"".equals(s)) {
			result = s.replaceAll("^[�?*| *| *|\\s*]*", "").replaceAll("[�?*| *| *|\\s*]*$", "");
		}
		return result;
	}

	/**
	 * 右对齐字符串，用指定字符串填充左边�?�可以指定编码集
	 * 
	 * @param str 要对齐的字符�?
	 * @param size 扩展字符串到指定宽度
	 * @param padStr 填充字符�?
	 *
	 * @return 如果字符串为<code>null</code>，则返回<code>null</code>
	 */
	public static String alignRightsByCharSet(String str, int size, String padStr, String charSet) {

		if (str == null) {
			return null;

		}

		String padStringFinal = (StringUtils.isEmpty(padStr)) ? EMPTY_STRING : padStr;
		int padLen = padStringFinal.length();
		int strLen = 0;
		if (StringUtils.isEmpty(charSet)) {
			strLen = str.getBytes().length;
		} else {
			try {
				strLen = str.getBytes(charSet).length;
			} catch (UnsupportedEncodingException e) {
				strLen = str.getBytes().length;
			}
		}
		int pads = size - strLen;
		if (pads <= 0) {
			return str;
		}

		if (pads == padLen) {
			return padStringFinal.concat(str);
		} else if (pads < padLen) {
			return padStringFinal.substring(0, pads).concat(str);
		} else {
			char[] padding = new char[pads];
			char[] padChars = padStringFinal.toCharArray();

			for (int i = 0; i < pads; i++) {
				padding[i] = padChars[i % padLen];
			}

			return new String(padding).concat(str);
		}

	}

	/**
	 * 参�?�号右对齐补�?12�?
	 * 
	 * @param str
	 * @return
	 */
	public static String alignRightsWithRrn(String str) {
		return alignRightsByCharSet(str, 12, "0", null);
	}

	/**
	 * 16进制转化为字符串
	 */
	public static String hexToString(String hex) {
		try {
			byte[] hexBytes = new byte[hex.length() / 2];
			for (int i = 0; i < hexBytes.length; i++) {
				hexBytes[i] = (byte) (0xff & Integer.parseInt(hex.substring(i * 2, i * 2 + 2), 16));
			}
			return new String(hexBytes, "UTF-8");
		} catch (Exception e) {
			LOGGER.error(e.getMessage(), e);
		}
		return null;
	}

}
